package com.action;

import com.opensymphony.xwork2.ModelDriven;

import com.model.Users;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.dao.*;

public class UsersAction implements ModelDriven<Users>,ServletRequestAware{
	private Users users;
	private HttpServletRequest request;
	UsersDAO usersDao=new UsersDAO();
	public String login() throws Exception{
		if(usersDao.get(users.getUsername(),users.getPassword())!=null) {
			return "success";
		}else {
		return "error";
		}
	}
	public String register() throws Exception{
		if(usersDao.get(users.getUsername(),users.getPassword())==null) {
			usersDao.insert(users.getUsername(),users.getPassword());
			return"success";
		}else {
		return "error";
		}
	}
	public String update() throws Exception{
		
			usersDao.update(users);
			return"success";
		
	}
	public String delete() {
		String sid =request.getParameter("id");
		usersDao.delete(Integer.parseInt(sid));
		return "success";
	}
	public Users getUsers() {
		return users;
	}
	public void setUsers(Users users) {
		this.users = users;
	}
	@Override
	public Users getModel() {
		users=new Users();
		return this.getUsers();		
	}
	public String show() {
		List<Users> userslist = usersDao.getAll();
		request.setAttribute("userslist", userslist);
		return"success";
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
		
	}
	
}

